package com.empire.module.member.controller.admin.mapconfig;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.mapconfig.vo.*;
import com.empire.module.member.dal.dataobject.mapconfig.MapConfigDO;
import com.empire.module.member.service.mapconfig.MapConfigService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 地图配置主")
@RestController
@RequestMapping("/member/map-config")
@Validated
public class MapConfigController {

    @Resource
    private MapConfigService mapConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建地图配置主")
    @PreAuthorize("@ss.hasPermission('member:map-config:create')")
    public CommonResult<String> createMapConfig(@Valid @RequestBody MapConfigSaveReqVO createReqVO) {
        return success(mapConfigService.createMapConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地图配置主")
    @PreAuthorize("@ss.hasPermission('member:map-config:update')")
    public CommonResult<Boolean> updateMapConfig(@Valid @RequestBody MapConfigSaveReqVO updateReqVO) {
        mapConfigService.updateMapConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地图配置主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:map-config:delete')")
    public CommonResult<Boolean> deleteMapConfig(@RequestParam("id") String id) {
        mapConfigService.deleteMapConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得地图配置主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:map-config:query')")
    public CommonResult<MapConfigRespVO> getMapConfig(@RequestParam("id") String id) {
        MapConfigDO mapConfig = mapConfigService.getMapConfig(id);
        return success(BeanUtils.toBean(mapConfig, MapConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得地图配置主分页")
//    @PreAuthorize("@ss.hasPermission('member:map-config:query')")
    public CommonResult<PageResult<MapConfigRespVO>> getMapConfigPage(@Valid MapConfigPageReqVO pageReqVO) {
        PageResult<MapConfigDO> pageResult = mapConfigService.getMapConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MapConfigRespVO.class));
    }

    @GetMapping("/configSelect")
    @Operation(summary = "获得配置下拉框")
//    @PreAuthorize("@ss.hasPermission('member:map-config:query')")
    public CommonResult<List<WarnConfigRespVO>> getConfigSelect(@Valid WarnConfigListVo reqVO) {
        List<MapConfigDO> result = mapConfigService.getConfigSelect(reqVO);
        return success(BeanUtils.toBean(result, WarnConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出地图配置主 Excel")
    @PreAuthorize("@ss.hasPermission('member:map-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapConfigExcel(@Valid MapConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MapConfigDO> list = mapConfigService.getMapConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "地图配置主.xls", "数据", MapConfigRespVO.class,
                        BeanUtils.toBean(list, MapConfigRespVO.class));
    }

}